﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using TradingAppDal;
using System.Collections.Concurrent;
using System.Windows.Forms;

namespace TradingApp.Helper
{
    public static class ThreadHelper
    {
        public static object objToProcessEntityLock = new object();
        public static void AddInToProcessEntityList(BindingList<EntityDal> lsttToAdd, List<EntityDal> lstEntityDal, ConcurrentDictionary<long, EntityDal> DictEntitiesInQueue, GroupBox gbEntitiesToProcess)
        {
            lock (objToProcessEntityLock)
            {
                foreach (EntityDal oEntity in lstEntityDal)
                {
                    if (!DictEntitiesInQueue.ContainsKey(oEntity.Process_ID.Value))
                    {
                        DictEntitiesInQueue.TryAdd(oEntity.Process_ID.Value, oEntity);
                        lsttToAdd.Add(oEntity);
                    }

                }
                gbEntitiesToProcess.Text = "Entities To Process - " + lsttToAdd.Count;
            }
        }
        public static void RemoveFromToProcessEntityList(BindingList<EntityDal> lsttToRemove, EntityDal oEntityDal, UIAction oAction, GroupBox gbEntitiesToProcess)
        {
            lock (objToProcessEntityLock)
            {
                RemoveEntityByProcessID(lsttToRemove, oEntityDal);
                gbEntitiesToProcess.Text = "Entities To Process - " + lsttToRemove.Count;
            }
        }

        private static void RemoveEntityByProcessID(BindingList<EntityDal> lsttToRemove, EntityDal oEntityDal)
        {
            int index = -1;
            for (int i = 0; i < lsttToRemove.Count; i++)
            {
                if (lsttToRemove[i].Process_ID == oEntityDal.Process_ID)
                {
                    index = i;
                    break;
                }
            }
            if (index > -1)
                lsttToRemove.RemoveAt(index);
        }

        public static object objToProcessSecurityLock = new object();
        public static void AddInToProcessSecurityList(BindingList<SecurityDal> lsttToAdd, List<SecurityDal> lstSecurityDal, EntityDal parentEntity, ConcurrentQueue<SecurityDal> SecurityQueueToProcess, GroupBox gbEntitiesToProcess)
        {
            lock (objToProcessSecurityLock)
            {
                foreach (SecurityDal oSecurityDal in lstSecurityDal)
                {
                    oSecurityDal.ParentEntity = parentEntity;
                    SecurityQueueToProcess.Enqueue(oSecurityDal);
                    lsttToAdd.Add(oSecurityDal);
                }
                gbEntitiesToProcess.Text = "Securities To Process - " + lsttToAdd.Count;
            }
        }
        public static void AddInToProcessSecurityList1(BindingList<SecurityDal> lsttToAdd, SecurityDal oSecurityDal)
        {
            lock (objToProcessSecurityLock)
            {
                lsttToAdd.Add(oSecurityDal);
            }
        }
        public static void RemoveFromToProcessSecurityList(BindingList<SecurityDal> lsttToRemove, SecurityDal oSecurityDal, GroupBox gbSecuritiesToProcess)
        {
            lock (objToProcessSecurityLock)
            {
                lsttToRemove.Remove(oSecurityDal);
                gbSecuritiesToProcess.Text = "Securities To Process - " + lsttToRemove.Count;
            }
        }

        public static object objInProcessEntityLock = new object();
        public static void AddInInProcessEntityList(BindingList<EntityDal> lsttToAdd, EntityDal oEntityDal, GroupBox gbEntitiesInProcess)
        {
            lock (objInProcessEntityLock)
            {
                lsttToAdd.Add(oEntityDal);
                gbEntitiesInProcess.Text = "Entities In Process - " + lsttToAdd.Count;
            }
        }
        public static void RemoveFromInProcessEntityList(BindingList<EntityDal> lsttToAdd, EntityDal oEntityDal, GroupBox gbEntitiesInProcess)
        {
            lock (objInProcessEntityLock)
            {
                lsttToAdd.Remove(oEntityDal);
                gbEntitiesInProcess.Text = "Entities In Process - " + lsttToAdd.Count;
            }
        }

        public static object objInProcessSecurityLock = new object();
        public static void AddInInProcessSecurityList(BindingList<SecurityDal> lsttToAdd, SecurityDal oSecurityDal, GroupBox gbSecuritiesInProcess)
        {
            lock (objInProcessSecurityLock)
            {
                lsttToAdd.Add(oSecurityDal);
                gbSecuritiesInProcess.Text = "Securities In Process - " + lsttToAdd.Count;
            }
        }
        public static void RemoveFromInProcessSecurityList(BindingList<SecurityDal> lsttToAdd, SecurityDal oSecurityDal, GroupBox gbSecuritiesInProcess)
        {
            lock (objInProcessSecurityLock)
            {
                lsttToAdd.Remove(oSecurityDal);
                gbSecuritiesInProcess.Text = "Securities In Process - " + lsttToAdd.Count;
            }
        }

        public static object objProcessedEntityLock = new object();
        public static void AddInProcessedEntityList(BindingList<EntityDal> lsttToAdd, EntityDal oEntityDal, GroupBox gbEntitiesProcesed)
        {
            lock (objProcessedEntityLock)
            {
                lsttToAdd.Add(oEntityDal);
                gbEntitiesProcesed.Text = "Entities Processed - " + lsttToAdd.Count;
            }
        }
        public static void RemoveFromProcessedEntityList(BindingList<EntityDal> lsttToRemove, EntityDal oEntityDal, GroupBox gbEntitiesProcesed)
        {
            lock (objProcessedEntityLock)
            {
                RemoveEntityByProcessID(lsttToRemove, oEntityDal);
                //lsttToRemove.Remove(oEntityDal);
                gbEntitiesProcesed.Text = "Entities Processed - " + lsttToRemove.Count;
            }
        }
        public static object objProcessedSecurityLock = new object();
        public static void AddInProcessedSecurityList(BindingList<SecurityDal> lsttToAdd, SecurityDal oSecurityDal, GroupBox gbSecuritiesProcessed)
        {
            lock (objProcessedSecurityLock)
            {
                lsttToAdd.Add(oSecurityDal);
                gbSecuritiesProcessed.Text = "Securities Processed - " + lsttToAdd.Count;
            }
        }
        public static void RemoveFromProcessedSecurityList(BindingList<SecurityDal> lsttToRemove, SecurityDal oSecurityDal, GroupBox gbSecuritiesProcessed)
        {
            lock (objProcessedSecurityLock)
            {
                lsttToRemove.Remove(oSecurityDal);
                gbSecuritiesProcessed.Text = "Securities Processed - " + lsttToRemove.Count;
            }
        }
        public static void RemoveFromProcessedSecurityList(BindingList<SecurityDal> lstToRemove, long Process_ID, GroupBox gbSecuritiesProcessed)
        {
            lock (objProcessedSecurityLock)
            {
                List<SecurityDal> lstSec = lstToRemove.ToList().FindAll(p=> p.Process_ID.Value == Process_ID);
                foreach (SecurityDal Sec in lstSec)
                {
                    lstToRemove.Remove(Sec);
                }
                gbSecuritiesProcessed.Text = "Securities Processed - " + lstToRemove.Count;
            }
        }

        public static SecurityDal DequeueFromInProcessSecurityList(BindingList<SecurityDal> lsttToRemove)
        {
            SecurityDal oSecurityDal = null;
            lock (objInProcessSecurityLock)
            {
                if (lsttToRemove.Count > 0)
                {
                    oSecurityDal = lsttToRemove[0];
                    lsttToRemove.Remove(oSecurityDal);
                }
            }
            return oSecurityDal;
        }
        public static object objProcessedErrorLock = new object();
        public static void AddInProcessedWithErrorSecurityList(BindingList<SecurityDal> lsttToAdd, SecurityDal oSecurityDal, GroupBox gbSecuritiesError)
        {
            lock (objProcessedErrorLock)
            {
                lsttToAdd.Add(oSecurityDal);
                gbSecuritiesError.Text = "Securities Processed with Error - " + lsttToAdd.Count;
            }
        }

    }
}
